#include "settings.h"
#include "system.h"
#include <algorithm>

namespace doc
{
    static const char * REG_KEY = "Software\\Navigo\\XmlEd";
    static const char * TEMPLATE_DIR = "\\structures\\";
    static const char * TEMPLATE_EXTENSION = ".xml";
}
namespace doc
{

using sys::RegistryKey;
using sys::runtime_error_ex;
using std::string;
using std::runtime_error;

Settings::Settings()
{
}

Settings::~Settings()
{
}


void Settings::Load()
{
	RegistryKey r(REG_KEY);
	XSplit = r.GetIntValue("XSplit");
	YSplit = r.GetIntValue("YSplit");
}

void Settings::Save()
{
	RegistryKey r(REG_KEY);
	r.SetValue("XSplit",XSplit);
	r.SetValue("YSplit",YSplit);
}

const string_basket &Settings::GetTemplates() const
{
	if (_templates.size() == 0)
 	{
	 	WIN32_FIND_DATA find;
	 	string mask = _exePath;
   		mask  += TEMPLATE_DIR;
   		mask  += "*";
     	mask  += TEMPLATE_EXTENSION;
	 	HANDLE find_handle = FindFirstFile(mask.c_str(), &find);
	 	if (find_handle == INVALID_HANDLE_VALUE)
	 		throw runtime_error_ex("No structure definitions found: '%s'"
	   								,mask.c_str());
		do
		{
	        char * fname = find.cFileName;
         	const int len = strlen(fname);
         	// does the filename end with .xml?
			if (fname[len-4] == '.')
			{
		        fname[len-4] = '\0';
				_templates.push_back(find.cFileName);
			}
		} while (FindNextFile(find_handle,&find));

	 	FindClose(find_handle);

	 	// sort result
	    sort(_templates.begin(),_templates.end());
   }
    return _templates;
}

void Settings::SetExePath(const string &path)
{
	_exePath = path;
}

void Settings::GetTemplateFilename(int index, string& result) const
{
	GetTemplates();
	result = GetTemplateFilename(_templates[index]);
}

string Settings::GetTemplateFilename(const string& templateName) const
{
	return TemplatePath() +templateName+TEMPLATE_EXTENSION;
}

string Settings::TemplatePath() const
{
	string result = _exePath;
 	result += TEMPLATE_DIR;
 	return result;
}

void Settings::GetTextFont(LOGFONT &font) const
{
	RegistryKey r(REG_KEY);
   bool hasValues = r.GetIntValue("fHas") > 0;
   if (!hasValues)
   {
  	  ZeroMemory(&font,sizeof(font));
      return;
   }
  font.lfHeight = r.GetIntValue("lfHeight");
  font.lfWidth = r.GetIntValue("lfWidth");
  font.lfEscapement = r.GetIntValue("lfEscapement");
  font.lfOrientation = r.GetIntValue("lfOrientation");
  font.lfWeight = r.GetIntValue("lfWeight");
  font.lfItalic = r.GetIntValue("lfItalic");
  font.lfUnderline = r.GetIntValue("lfUnderline");
  font.lfStrikeOut = r.GetIntValue("lfStrikeOut");
  font.lfCharSet = r.GetIntValue("lfCharSet");
  font.lfOutPrecision = r.GetIntValue("lfOutPrecision");
  font.lfClipPrecision = r.GetIntValue("lfClipPrecision");
  font.lfQuality = r.GetIntValue("lfQuality");
  font.lfPitchAndFamily = r.GetIntValue("lfPitchAndFamily"); 
   r.GetValue("lfFaceName",font.lfFaceName, LF_FACESIZE);

}

void Settings::SetTextFont(const LOGFONT &font)
{
	RegistryKey r(REG_KEY);
   r.SetValue("fHas",1);
   r.SetValue("lfHeight", font.lfHeight);
   r.SetValue("lfWidth", font.lfWidth);
   r.SetValue("lfEscapement", font.lfEscapement);
   r.SetValue("lfOrientation", font.lfOrientation);
   r.SetValue("lfWeight", font.lfWeight);
   r.SetValue("lfItalic", font.lfItalic);
   r.SetValue("lfUnderline", font.lfUnderline);
   r.SetValue("lfStrikeOut", font.lfStrikeOut);
   r.SetValue("lfCharSet", font.lfCharSet);
   r.SetValue("lfOutPrecision", font.lfOutPrecision);
   r.SetValue("lfClipPrecision", font.lfClipPrecision);
   r.SetValue("lfQuality", font.lfQuality);
   r.SetValue("lfPitchAndFamily", font.lfPitchAndFamily); 
   r.SetValue("lfFaceName", font.lfFaceName);
	
}

}
